package com.jisf.driver.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jisf.driver.entity.enumtype.DelFlagEnum;
import com.jisf.driver.entity.enumtype.EnumContext;
import com.jisf.driver.entity.enumtype.StatusEnum;
import com.jisf.driver.entity.po.Role;
import com.jisf.driver.entity.vo.MenuListVo;
import com.jisf.driver.entity.vo.RoleAddRequestVo;
import com.jisf.driver.entity.vo.RoleListVo;
import com.jisf.driver.exception.SystemException;
import com.jisf.driver.mapper.RoleMapper;
import com.jisf.driver.response.IErrorCode;
import com.jisf.driver.response.ResultCode;
import com.jisf.driver.service.IMenuService;
import com.jisf.driver.service.IRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author jisf
 * @since 2022-04-08
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private IMenuService iMenuService;

    /**
     * 获取角色列表
     * @return
     */
    @Override
    public List<RoleListVo> roleList() {
        try{
            List<RoleListVo> roleListVos = roleMapper.selectRoleList();
            for (RoleListVo role : roleListVos) {
                List<MenuListVo> menuLists = role.getMenus();
                List<MenuListVo> menuListVos = iMenuService.organizeMenu(menuLists);
                role.setMenus(menuListVos);
            }
            return roleListVos;
        }catch (Exception e){
            throw new SystemException(e);
        }
    }

    @Override
    public IErrorCode roleAdd(RoleAddRequestVo roleAddRequestVo) {
        Role role = new Role();
        BeanUtils.copyProperties(roleAddRequestVo,role);
        String status = roleAddRequestVo.getStatus();
        if (StringUtils.isNotBlank(status)) {
            role.setStatus(StatusEnum.getEnum(status));
        } else {
            role.setStatus(StatusEnum.UNABLE);
        }
        role.setDelFlag(DelFlagEnum.NO_DELETE);
        int insert = roleMapper.insert(role);
        return insert == 0 ? ResultCode.ROLE_ADD_FAILED : ResultCode.ROLE_ADD_SUCCESS;
    }

    /**
     * 获取所有角色
     * @return
     */
    @Override
    public List<RoleListVo> roleAll() {
        try {
            return roleMapper.selectAllRoles();
        }catch (Exception e){
            throw new SystemException(e);
        }
    }

    /**
     * 改变角色状态
     * @param roleId
     * @param status
     * @return
     */
    @Override
    public IErrorCode roleChangeStatus(Integer roleId, String status) {
        try {
            UpdateWrapper<Role> updateWrapper = new UpdateWrapper<>();
            updateWrapper.set("status", StatusEnum.getEnum(status));
            updateWrapper.eq("id", roleId);
            int update = roleMapper.update(new Role(), updateWrapper);
            return update == 0 ? ResultCode.ROLE_STATUS_UPDATE_FAILED : ResultCode.ROLE_STATUS_UPDATE_SUCCESS;
        }catch (Exception e){
            throw new SystemException(e);
        }
    }

    /**
     * 删除角色
     * @param roleId
     * @return
     */
    @Override
    public IErrorCode roleDelete(Integer roleId) {
        UpdateWrapper<Role> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", roleId);
        updateWrapper.set("del_flag", DelFlagEnum.HAVE_DELETE);
        int update = roleMapper.update(new Role(), updateWrapper);
        return update == 0 ? ResultCode.ROLE_DEL_FLAG_FAILED : ResultCode.ROLE_DEL_FLAG_SUCCESS;
    }

    /**
     * 修改角色
     * @param roleListVo
     * @return
     */
    @Override
    public IErrorCode roleUpdate(RoleListVo roleListVo) {
        Role role = new Role();
        BeanUtils.copyProperties(roleListVo, role);
        int update = roleMapper.updateById(role);
        return update == 0 ? ResultCode.ROLE_UPDATE_FAILED : ResultCode.ROLE_UPDATE_SUCCESS;
    }
}
